const Koa = require('koa')
const axios = require('axios')
const router = require('koa-router')()
const cors = require('koa2-cors') // 解决跨域问题
const app = new Koa()
let _dissData = null
let _singerData = null
let _lyricData = null
let _dissDetailData = null
let _searchResult = null
let _vKeyInfo = null
router
  .get('/getDissList', async (ctx, next) => {
    let url = 'https://c.y.qq.com/splcloud/fcgi-bin/fcg_get_diss_by_tag.fcg'
    await axios
      .get(url, {
        headers: {
          referer: 'https://c.y.qq.com/',
          host: 'c.y.qq.com'
        },
        params: ctx.query
      })
      .then((res) => {
        _dissData = res.data
      })
      .catch((err) => {
        console.log(err)
      })
    ctx.response.status = 200
    ctx.body = _dissData
  })
  
  .get('/getSingerList', async (ctx, next) => {
    let url = 'https://u.y.qq.com/cgi-bin/musicu.fcg'
    await axios
      .get(url, {
        headers: {
          referer: 'https://c.y.qq.com/',
          host: 'u.y.qq.com'
        },
        params: ctx.query
      })
      .then((res) => {
        _singerData = res.data
      })
      .catch((err) => {
        console.log(err)
      })
    ctx.response.status = 200
    ctx.body = _singerData
  })

  .get('/getLyric', async (ctx, next) => {
    
    let url = 'https://c.y.qq.com/lyric/fcgi-bin/fcg_query_lyric.fcg'
    await axios
      .get(url, {
        headers: {
          referer: 'https://y.qq.com/n/yqq/song/003OUlho2HcRHC.html',
          host: 'u.y.qq.com'
        },
        params: ctx.query
      })
      .then((res) => {
        _lyricData = res.data
        if(typeof _lyricData === 'string') {
          let reg = /^\w+\(({[^()]+})\)$/
          let matches = _lyricData.match(reg)
          if(matches) {
            _lyricData = JSON.parse(matches[1])
          }
        }
      })
      .catch((err) => {
        console.log(err)
      })
    ctx.response.status = 200
    ctx.body = _lyricData
  })

  .get('/getDissDetail', async (ctx, next) => {
    let url = 'https://c.y.qq.com/qzone/fcg-bin/fcg_ucc_getcdinfo_byids_cp.fcg'
    await axios
      .get(url, {
        headers: {
          referer: 'https://y.qq.com/n/yqq/playlist/1169458991.html',
          host: 'u.y.qq.com'
        },
        params: ctx.query
      })
      .then((res) => {
        _dissDetailData = res.data
      })
      .catch((err) => {
        console.log(err)
      })
    ctx.response.status = 200
    ctx.body = _dissDetailData
  })

  .get('/getSearchResult', async (ctx, next) => {
    let url = 'https://c.y.qq.com/soso/fcgi-bin/search_for_qq_cp'
    
    await axios
      .get(url, {
        headers: {
          referer: 'https://y.qq.com/m/index.html',
          host: 'u.y.qq.com'
        },
        params: ctx.query
      })
      .then((res) => {
        _searchResult = res.data
      })
      .catch((err) => {
        console.log(err)
      })
      
    ctx.response.status = 200
    ctx.body = _searchResult
  })

  .get('/getVkey', async (ctx, next) => {
    let url = 'https://c.y.qq.com/base/fcgi-bin/fcg_music_express_mobile3.fcg'
    
    await axios
      .get(url, {
        headers: {
          referer: 'https://y.qq.com/portal/player.html',
          host: 'u.y.qq.com'
        },
        params: ctx.query
      })
      .then((res) => {
        _vKeyInfo = res.data
      })
      .catch((err) => {
        console.log(err)
      })
    ctx.response.status = 200
    ctx.body = _vKeyInfo
  })


app
  .use(cors({
    origin: function (ctx) {
        if (ctx.url === '/getDissList') {
            return false;
        }
        return '*';
    },
    exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'],
    maxAge: 5,
    credentials: true,
    allowMethods: ['GET', 'POST', 'DELETE'],
    allowHeaders: ['Content-Type', 'Authorization', 'Accept'],
  }))
  .use(router.routes())
  .use(router.allowedMethods())

app.listen(6677, () => {
  console.log('serve is start at port 6677')
}) 